package cn.gp.chatclient.ui.component;

import javax.swing.*;
import java.awt.*;

/**
 * @ClassName ChatBubble
 * @Description TODO
 * @Author He Guoping
 * @Date 2025/6/17 9:12
 * @Version JDK17
 */
public class ChatBubble extends JPanel {
    private boolean isMe;

    public ChatBubble(String sender, String message, String time, boolean isMe) {
        this.isMe = isMe;
        setLayout(new BorderLayout());
        setOpaque(false);
        setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));

        // 消息内容
        JTextArea textArea = new JTextArea(message);
        textArea.setLineWrap(true);
        textArea.setWrapStyleWord(true);
        textArea.setEditable(false);
        textArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));

        // 气泡面板
        JPanel bubblePanel = new JPanel(new BorderLayout());
        bubblePanel.setBorder(BorderFactory.createCompoundBorder(
                BorderFactory.createLineBorder(new Color(200, 200, 200)),
                BorderFactory.createEmptyBorder(8, 12, 8, 12)
        ));
        bubblePanel.setBackground(isMe ? new Color(150, 230, 150) : Color.WHITE);

        // 发送者信息
        JLabel senderLabel = new JLabel(sender + "  " + time);
        senderLabel.setFont(new Font("微软雅黑", Font.PLAIN, 10));
        senderLabel.setForeground(Color.GRAY);

        bubblePanel.add(senderLabel, BorderLayout.NORTH);
        bubblePanel.add(textArea, BorderLayout.CENTER);

        // 根据发送者调整布局
        if (isMe) {
            add(bubblePanel, BorderLayout.EAST);
        } else {
            add(bubblePanel, BorderLayout.WEST);
        }
    }
}
